<!--#include file="../include/conn.asp" -->
<!--#include file="../Include/Function.asp" -->
<%
dim action : action=getForm("action","get")
Select case action	
	case "del" : DelField
	case "add" : AddField
	case "edit" : editField
	case "save" :SaveField	
	case "order" :updateOrder	
End Select
dim FieldID, FieldName, FieldOrder, Field_title

dim sql, msg

'增加字段
Sub AddField 
	Fieldfl=getForm("Fieldfl","post")	
	FieldName=getForm("FieldName","post")
	FieldOrder=getForm("FieldOrder","post")
	Field_title=getForm("Field_title","post")	
	
	if isnul(FieldName) then alertMsgAndGo "参数名称不能为空，请修改","-1"
	if isnul(Field_title) then alertMsgAndGo "字段名称不能为空，请修改","-1"
	Dim rsObj	:	Set rsObj=conn.execute("select count(*) from FieldSet where Field_title='"&Field_title&"'","r1")
	if rsObj(0) >0 then alertMsgAndGo "字段名称已存在，请修改","-1"
	rsObj.close	:	Set rsObj = nothing
	conn.execute "ALTER TABLE Content ADD column "&Field_title&" Text(255)" ,"exe"	
	if Err then alertMsgAndGo "添加字段失败,请联系管理员","-1"
	conn.execute "insert into FieldSet(Fieldfl,FieldName,Field_title,FieldOrder) values("&Fieldfl&",'"&FieldName&"','"&Field_title&"',"&FieldOrder&")","exe"		
	alertMsgAndGo "添加成功","Pro_Field.asp?Field=admin&Fieldfl="&Fieldfl&""
End Sub	

'修改字段
Sub editField
	Fieldid=getForm("Fieldid","post")
 	Fieldfl=getForm("Fieldfl","post")
	FieldName=getForm("FieldName","post")
	FieldOrder=getForm("FieldOrder","post")
	Field_title=getForm("Field_title","post")	
	
	if isnul(FieldName) then alertMsgAndGo "参数名称不能为空，请修改","-1"
	'if isnul(Field_title) then alertMsgAndGo "字段名称不能为空，请修改","-1"
	Dim rsObj	:	Set rsObj=conn.execute("select * from FieldSet where Fieldid="&Fieldid&"","r1")
	if rsObj(4) =Field_title then alertMsgAndGo "字段名称已存在"&Field_title&"，请修改","-1"

	'conn.execute "alter table Content drop column "&rsObj(4)
	'conn.execute "alter table Content add column "&Field_title&" Text(255)" ,"exe"

	if Err then alertMsgAndGo "修改字段失败,请联系管理员","-1"
	conn.execute "update FieldSet set Fieldfl="&Fieldfl&", FieldName='"&FieldName&"',FieldOrder="&FieldOrder&" where Fieldid="&Fieldid, "exe"
		
	alertMsgAndGo "修改成功","Pro_Field.asp?Field=admin&Fieldfl="&Fieldfl&""
	rsObj.close	:	Set rsObj = nothing	
End Sub	
Function sortTypeName()
sortTypeName=SortNames(Fieldfl)
End Function

function FielName(Fieldfl)
	sql="select * from zych_Type where SortID="&Fieldfl
	set rs=conn.execute(sql,"r1")
    FielName=rs("SortName")
end function
'字段列表
Sub FieldList(Fieldfl)
	Dim rsObj:Set rsObj=conn.execute("select FieldID,Fieldfl,FieldName,Field_title,FieldOrder from FieldSet where Fieldfl="&Fieldfl&" Order by FieldOrder Asc,FieldID","r1")
	If rsObj.Eof Then 
		echo"<tr bgcolor=""#FFFFFF"" align=""center"">"&vbcrlf& _
			"<td colspan=""7"">没有数据</td>"&vbcrlf& _
		  "</tr>"&vbcrlf
	Else
		Do while not rsObj.Eof
		 echo "<tr bgcolor=""#FFFFFF"" align=""center"" onMouseOver=""this.bgColor='#EEEEEE'"" onMouseOut=""this.bgColor='#ffffff'"">"
		 echo "<td height=""28"" class=td>"
		 echo "<input type=""checkbox"" name=""id"" value="""&rsObj("FieldID")&""" class=""checkbox"" onclick='silbingCheck(this)'/>"&vbcrlf
		 echo "<input type=""checkbox"" name=""Field_title"" value="""&rsObj("Field_title")&""" style=""display:none"" />"&vbcrlf
		 echo "<input type=""hidden"" name=""FieldIDs"" value="""&rsObj("FieldID")&""" /><input type=""hidden"" name=""Fieldfl"" value="""&rsObj("Fieldfl")&""" /></td>"&vbcrlf
		 echo "<td class=td><input type=""text"" readonly style=""text-align:center; width:40px"" value="""&rsObj("FieldID")&""" class=""inp""/></td>"&vbcrlf
		 echo "<td class=td>"&FielName(rsObj("Fieldfl"))&"</td>" &vbcrlf
		 echo "<td class=td>"&rsObj("FieldName")&"</td>" &vbcrlf
		 echo "<td class=td>"&rsObj("Field_title")&"</td>" &vbcrlf
		 echo "<td class=td>&nbsp;</td>" &vbcrlf
		 echo "<td class=td><input class=""inp"" style=""width:30px"" name=""FieldOrders"" value="""&rsObj("FieldOrder")&"""/></td>"&vbcrlf
		 echo "<td class=td><input type=""button"" value=""修 改"" onClick=""window.location.href='?field=xiugai&FieldID="&rsObj("FieldID")&"&Fieldfl="&rsObj("Fieldfl")&"'""  class=""btn""/> | <input type=""button"" name=""Submit"" value=""删 除"" onClick=""javascript:if(confirm('确定删除？删除后不可恢复!')){window.location.href='?action=del&id="&rsObj("FieldID")&"&Fieldfl="&rsObj("Fieldfl")&"&Field_title="&rsObj("Field_title")&"';}else{history.go(0);}""  class=""btn""/></td>"
		 echo "</tr>"
		  rsObj.MoveNext
		Loop
	End If
	rsObj.close : Set rsObj = nothing
End Sub

'删除字段
Sub DelField 
	Dim sql
	dim ID 	:	ID = getForm("id","both")
	dim Fieldfl:Fieldfl=request.QueryString("Fieldfl")
	dim Field_title:Field_title=getForm("Field_title","both")
	dim m_Field_title:m_Field_title=replace(Field_title,"&apos;","'")
	Field_title = replace(Field_title,"&apos;","")
	sql ="alter table Content drop column "&Field_title
	conn.execute (sql)	
	if instr(m_Field_title,"'")=0 then m_Field_title = "'"&m_Field_title&"'"
	sql = "Delete * from FieldSet where Field_title in ("&m_Field_title&")"
	conn.execute (sql)
	alertMsgAndGo "删除成功了","Pro_Field.asp?Field=admin&Fieldfl="&Fieldfl&""	
	
	if Err then echo err.description
	
End Sub 

'更新排序
Sub updateOrder
	Dim ids			:	ids=split(getForm("FieldIDs","post"),",")
	dim Fieldfl 	:	Fieldfl=request.QueryString("Fieldfl")
	Dim orders		:	orders=split(getForm("FieldOrders","post"),",")
	If Ubound(ids)=-1 Then 	'防止有值为空时下标越界
		ReDim ids(0)
		ids(0)=""
	End If	
	
	If Ubound(orders)=-1 Then
		ReDim orders(0)
		orders(0)=0
	End If
	Dim i
	
	For i=0 To Ubound(ids)		
		if isnum(trim(orders(i))) then
			conn.execute "update FieldSet Set FieldOrder="&trim(orders(i))&" Where FieldID="&trim(ids(i)),"exe"	
		else
			conn.execute "update FieldSet Set FieldOrder=0 Where FieldID="&trim(ids(i)),"exe"	
		end if
	Next
	alertMsgAndGo "更新排序成功","Pro_Field.asp?Field=admin&Fieldfl="&Fieldfl&""
End Sub
%>